import socket

# 通过udp,获取本机ip
def get_host_ip():
    try:
        con = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        con.connect(('8.8.8.8', 80))
        ip = con.getsockname()[0]
        con.close()
        return ip
    except:
        print('This is null')


if __name__ == '__main__':
    ip = get_host_ip()
    print(ip)